package com.bear.fastrepair.mvvm.ui.manager

import android.annotation.SuppressLint
import android.content.Intent
import android.net.Uri
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bear.fastrepair.base.BaseViewHolder
import com.bear.fastrepair.databinding.ItemOrderBinding
import com.bear.fastrepair.entity.OrderRecord
import com.blankj.utilcode.util.ActivityUtils

class OrderAdapter(private val data: ArrayList<OrderRecord>) :
    RecyclerView.Adapter<OrderAdapter.RecordViewHolder>() {
    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): RecordViewHolder {
        return RecordViewHolder(
            ItemOrderBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
        )
    }

    override fun onBindViewHolder(holder: RecordViewHolder, position: Int) {
        if (data.isEmpty()) return
        holder.bind(data[position], position)
    }

    override fun getItemCount(): Int {
        return data.size
    }

    inner class RecordViewHolder(private val binding: ItemOrderBinding) :
        BaseViewHolder<OrderRecord>(binding.root) {
        @SuppressLint("SetTextI18n")
        override fun bind(item: OrderRecord, index: Int) {
            binding.orderTitle.text = item.description
            binding.address.text = item.locationAddress + item.locationAddress
            binding.clientName.text = item.clientName
            binding.clientTel.text = item.clientTel
            binding.callLl.setOnClickListener {
                val intent = Intent().apply {
                    action = Intent.ACTION_CALL
                    data = Uri.parse("tel:${item.clientTel}")
                }
                ActivityUtils.startActivity(intent)
            }
            binding.orderStatus.text = item.orderStatusText
            binding.engineerTel.text = item.employeeName
            binding.orderNumber.text = item.id
        }
    }
}